package com.example.repository;

import com.example.data.leave.Leave;
import com.example.data.leave.LeaveBO;
import com.example.data.leave.LeaveDO;
import com.example.mappers.LeaveRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public class LeaveRepository {// need to change with code
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //查询所有请假信息
    public List<Leave> getAllLeaveInfo(){
        String sql = "SELECT * FROM leave ORDER BY appid";
        List<Leave> leaveList = jdbcTemplate.query(sql, new LeaveRowMapper());
        return leaveList;
    }

    //根据empID查询个人请假信息
    public Leave getLeaveInfoByEmpID(long empId){
        String sql = "SELECT * FROM leave WHERE appId=? ";
        Leave leave = jdbcTemplate.queryForObject(sql, new LeaveRowMapper(), empId);
        return leave;
    }
    //新增请假信息
    public Leave addLeaveInfo(Leave leave){
        String sql = "INSERT INTO leave (appId,empId,empName,empDeptId,empDeptName,reason,leaveType,state) VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql, leave.getAppID(),leave.getEmpID(),leave.getEmpName(),leave.getEmpDeptID(),leave.getEmpDeptName(),
                leave.getReason(),leave.getType(),leave.getState());
        String sql2 = "SELECT * FROM leave WHERE empId=? ";
        Leave leave2 = jdbcTemplate.queryForObject(sql2, new LeaveRowMapper(), leave.getEmpID());
        return leave2;
    }
    public Leave updateLeaveById(long empId,Leave leave){//此可用于更新
        getLeaveInfoByEmpID(empId);//先显示请假状态信息
        String sql = "UPDATE leave set appId = ?,empName = ?,empDeptId = ?,empDeptName = ?,reason = ?,leaveType = ?,state = ? WHERE empId=?";
        jdbcTemplate.update(sql, leave.getAppID(),leave.getEmpID(),leave.getEmpName(),leave.getEmpDeptID(),leave.getEmpDeptName(),
                leave.getReason(),leave.getType(),leave.getState());
        return getLeaveInfoByEmpID(empId);
    }
    //删除请假信息
    public void deleteLeaveInfo(long appID, long empID){
        String sql = "DELETE FROM leave WHERE appId=? AND empId=?";
        jdbcTemplate.update(sql, appID,empID);
    }


    //查询某条请假信息,以map返回
    //todo
    public Map<String, Object> QueryLeaveInfoByID(long empID, long appID){
        Map<String, Object> leaveMap = new LinkedHashMap<>();
        leaveMap.put("appID", empID);
        leaveMap.put("empID", appID);
        leaveMap.put("empName", "33");
        leaveMap.put("empDeptID", 44L);
        leaveMap.put("empDeptName", "55");
        leaveMap.put("reason", "66");
        leaveMap.put("type", 77);
        leaveMap.put("state", 88);
        return leaveMap;
    }
    //修改某条请假信息
    //todo
    public List<Leave> editLeaveInfo(Leave leaveBO){
        return getFakeDO();
    }
    public List<Leave> getFakeDO(){
        Leave leaveInfo = new LeaveDO();
        leaveInfo.setState(11);
        leaveInfo.setType(22);
        leaveInfo.setReason("33");
        leaveInfo.setEmpDeptName("44");
        leaveInfo.setEmpName("55");
        leaveInfo.setAppID(66L);
        leaveInfo.setEmpID(77L);
        leaveInfo.setEmpDeptID(88L);
        return Arrays.asList(leaveInfo);
    }
}
